//
//  PaypwdViewController.m
//  WaywardPoint_Code
//
//  Created by 韩盼盼 on 15/9/28.
//  Copyright (c) 2015年 易网新科技. All rights reserved.
//

#import "PaypwdViewController.h"
#import "AFFNumericKeyboard.h"
#import "SurePaypwdController.h"
#define PayCount 6

@interface PaypwdViewController ()<AFFNumericKeyboardDelegate,HeadBackView>
{
    UIButton *gobackBtn;
    UIView *sureView;
}
@property(nonatomic,strong)SurePaypwdController *surepaypwdVc;
@property(nonatomic,strong)NSMutableArray *payArr;
@end

@implementation PaypwdViewController

-(SurePaypwdController *)surepaypwdVc
{
    if (!_surepaypwdVc) {
        _surepaypwdVc=[[SurePaypwdController alloc]init];
    }
    return _surepaypwdVc;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _payArr=[NSMutableArray array ];
    self.paypwdStr=@"";
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
    self.view.tag=1;
    self.title=@"设置交易密码";
    UIButton *leftBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0,40*PROPORTION_WIDTH, 54*PROPORTION_HEIGHT)];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"return_left"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(btnback) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarBtn=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem=leftBarBtn;
    
    [self showUI];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=NO;
    [self.payArr removeAllObjects];
    UITextField *pwdT1=(UITextField *)[self.view viewWithTag:300];
    UITextField *pwdT2=(UITextField *)[self.view viewWithTag:301];
    UITextField *pwdT3=(UITextField *)[self.view viewWithTag:302];
    UITextField *pwdT4=(UITextField *)[self.view viewWithTag:303];
    UITextField *pwdT5=(UITextField *)[self.view viewWithTag:304];
    UITextField *pwdT6=(UITextField *)[self.view viewWithTag:305];
    pwdT1.text=@"";
    pwdT2.text=@"";
    pwdT3.text=@"";
    pwdT4.text=@"";
    pwdT5.text=@"";
    pwdT6.text=@"";
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden=NO;
}
- (void)showUI
{
    UIView *contentView=[[UIView alloc]initWithFrame:CGRectMake(0, 60, ScreenW, ScreenH-60)];
    [self.view addSubview:contentView];
    
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake((ScreenW-150)*0.5, 10, 150, 25)];
    title.font=[UIFont systemFontOfSize:18];
    title.textAlignment=NSTextAlignmentCenter;
    title.text=@"请输入交易密码";
    title.textColor = COLOT_TEXTGRAY;
    [contentView addSubview:title];
    
    CGFloat btnH=50;
    
   //支付密码的框
    UIView *pwdview=[[UIView alloc]initWithFrame:CGRectMake(8, title.y+title.height+5, ScreenW-16, 50)];
    pwdview.layer.masksToBounds=YES;
    pwdview.layer.borderWidth=0.8;
    pwdview.layer.borderColor = [UIColor whiteColor].CGColor;
     CGFloat btnW=pwdview.width/6;
    [contentView addSubview:pwdview];
    for (int i=0; i<6; i++) {
        UITextField *pwdla=[[UITextField alloc]initWithFrame:CGRectMake(i*btnW, 0, btnW, btnH)];
        pwdla.textAlignment=NSTextAlignmentCenter;
        pwdla.secureTextEntry=YES;
        pwdla.textColor=[UIColor whiteColor];

        pwdla.enabled=NO;
        pwdla.keyboardType=UIKeyboardTypeNumberPad;
        pwdla.font=[UIFont systemFontOfSize:20];
        pwdla.layer.masksToBounds=YES;
        pwdla.layer.borderWidth=0.8;
        pwdla.layer.borderColor = [UIColor whiteColor].CGColor;
    
        pwdla.tag=i+300;
        [pwdview addSubview:pwdla];
    }
    
    UIView *keyboardview=[[UIView alloc]initWithFrame:CGRectMake(0, ScreenH-214, ScreenW, 216)];
        keyboardview.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:keyboardview];
    
    AFFNumericKeyboard *keyboard = [[AFFNumericKeyboard alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 216)];
    [keyboardview addSubview:keyboard];
    keyboard.delegate = self;
}

- (void)btnback
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)numberKeyboardBackspace
{
    [self clearpaypwd];
}

-(void)numberKeyboardInput:(NSInteger)number
{
    if (self.payArr.count< PayCount) {
        [self.payArr addObject:[NSString stringWithFormat:@"%ld",(long)number]];
        RXDLog(@"_payArr %@",_payArr);
        [self getpaypwd:self.payArr];
    }
}

-(NSArray *)substring:(NSString *)temp
{
    
    NSArray *array = [temp componentsSeparatedByString:@"^"]; //从字符A中分隔成2个元素的数组
    
    return array;
}
- (NSString *)getpaypwd:(NSArray *)pwdstrarr
{
    
    NSString *paypwdstr=@"";
    UITextField *pwdT1=(UITextField *)[self.view viewWithTag:300];
    UITextField *pwdT2=(UITextField *)[self.view viewWithTag:301];
    UITextField *pwdT3=(UITextField *)[self.view viewWithTag:302];
    UITextField *pwdT4=(UITextField *)[self.view viewWithTag:303];
    UITextField *pwdT5=(UITextField *)[self.view viewWithTag:304];
    UITextField *pwdT6=(UITextField *)[self.view viewWithTag:305];
    
    if (pwdT1.text.length==0) {
        pwdT1.text=pwdstrarr[0];
        RXDLog(@"pwdT1%@",pwdT1.text);
    }else if (pwdT2.text.length==0)
    {
        pwdT2.text=pwdstrarr[1];
        RXDLog(@"pwdT2%@",pwdT2.text);
    }else if (pwdT3.text.length==0)
    {
        pwdT3.text=pwdstrarr[2];
        RXDLog(@"pwdT3%@",pwdT3.text);
    }else if (pwdT4.text.length==0)
    {
        pwdT4.text=pwdstrarr[3];
        RXDLog(@"pwdT4%@",pwdT4.text);
    }else if (pwdT5.text.length==0)
    {
        pwdT5.text=pwdstrarr[4];
        RXDLog(@"pwdT5%@",pwdT5.text);
    }else if (pwdT6.text.length==0)
    {
        pwdT6.text=pwdstrarr[5];
        RXDLog(@"pwdT6%@",pwdT6.text);
    }
    if (pwdT1.text.length!=0&&pwdT2.text.length!=0&&pwdT3.text.length!=0
        &&pwdT4.text.length!=0&&pwdT5.text.length!=0&&pwdT6.text.length!=0) {
        paypwdstr=[NSString stringWithFormat:@"%@%@%@%@%@%@",pwdT1.text,pwdT2.text,pwdT3.text,pwdT4.text,pwdT5.text,pwdT6.text];
       
        self.paypwdStr=@"";
        self.surepaypwdVc.memberid=_memberid;
        self.surepaypwdVc.tempstr=paypwdstr;
        self.surepaypwdVc.payjumpStr=self.payjumpStr;

        [self.navigationController pushViewController:self.surepaypwdVc animated:YES];
        
    }
    
    return  paypwdstr;
}
- (void)clearpaypwd
{
    UITextField *pwdT1=(UITextField *)[self.view viewWithTag:300];
    UITextField *pwdT2=(UITextField *)[self.view viewWithTag:301];
    UITextField *pwdT3=(UITextField *)[self.view viewWithTag:302];
    UITextField *pwdT4=(UITextField *)[self.view viewWithTag:303];
    UITextField *pwdT5=(UITextField *)[self.view viewWithTag:304];
    UITextField *pwdT6=(UITextField *)[self.view viewWithTag:305];
    if (pwdT6.text.length!=0) {
        pwdT6.text=@"";
        [self.payArr removeLastObject];
    }else if (pwdT5.text.length!=0)
    {
        pwdT5.text=@"";
        [self.payArr removeLastObject];
    }else if (pwdT4.text.length!=0)
    {
        pwdT4.text=@"";
        [self.payArr removeLastObject];
    }else if (pwdT3.text.length!=0)
    {
        pwdT3.text=@"";
        [self.payArr removeLastObject];
    }else if (pwdT2.text.length!=0)
    {
        pwdT2.text=@"";
        [self.payArr removeLastObject];
    }else if (pwdT1.text.length!=0)
    {
        pwdT1.text=@"";
        [self.payArr removeLastObject];
    }
    
}

@end
